<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="78px">
      <el-form-item label="评论时间" prop="timeRange">
        <el-date-picker
          v-model="queryParams.timeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>

      <el-form-item label="评价状态" prop="isReply">
        <el-select v-model="queryParams.isReply" placeholder="请选择回复状态" clearable size="small">
          <el-option :label="'未回复'" :value="0" />
          <el-option :label="'已回复'" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="商品名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="用户名称" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="请输入用户名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                   v-hasPermi="['store:product-reply:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['store:product-reply:export']">导出</el-button>
      </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

            <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
            <el-table-column label="编号" align="center" prop="id" />
              <el-table-column label="商品信息" align="center" width="280">
                <template #default="{row}">
                  <div style="display: flex; align-items: center;">
                    <el-image
                      v-if="row.image"
                      style="width: 70px; height: 70px; cursor: pointer; flex-shrink: 0;"
                      :src="row.image"
                      :preview-src-list="[row.image]"
                      hide-on-click-modal
                    ></el-image>
                    <el-tooltip effect="light" placement="right">
                      <div
                        slot="content"
                        style="background:#fff;padding:16px;max-width:320px;border-radius:8px;box-shadow:0 2px 12px 0 rgba(0,0,0,0.1);word-break:break-all;line-height:1.7;"
                      >
                        {{ row.storeName }}
                      </div>
                      <span
                        style="margin-left: 10px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block;"
                        :title="row.storeName"
                      >
                        {{ row.storeName }}
                      </span>
                    </el-tooltip>
                  </div>
                </template>
              </el-table-column>
      <el-table-column label="用户名称" align="center" prop="nickname" width="120">
        <template v-slot="scope">
          <el-tooltip v-if="scope.row.nickname && scope.row.nickname.length > 10" effect="light" placement="top">
            <div slot="content">
              {{ scope.row.nickname }}
            </div>
            <span class="nickname-text">{{ scope.row.nickname }}</span>
          </el-tooltip>
          <span v-else class="nickname-text">{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品分数" align="center" prop="productScore" />
      <el-table-column label="服务分数" align="center" prop="serviceScore" />
      <el-table-column label="评论内容" align="center" prop="comment" width="200">
        <template v-slot="scope">
          <el-tooltip v-if="scope.row.comment && scope.row.comment.length > 20" effect="light" placement="top">
            <div slot="content" style="max-width: 300px; word-break: break-all;">
              {{ scope.row.comment }}
            </div>
            <span class="comment-text">{{ scope.row.comment || '_' }}</span>
          </el-tooltip>
          <span v-else class="comment-text">{{ scope.row.comment || '_' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回复内容" align="center" prop="merchantReplyContent" width="200">
        <template v-slot="scope">
          <el-tooltip v-if="scope.row.merchantReplyContent && scope.row.merchantReplyContent.length > 20" effect="light" placement="top">
            <div slot="content" style="max-width: 300px; word-break: break-all;">
              {{ scope.row.merchantReplyContent }}
            </div>
            <span class="comment-text">{{ scope.row.merchantReplyContent || '_' }}</span>
          </el-tooltip>
          <span v-else class="comment-text">{{ scope.row.merchantReplyContent || '_' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                     v-hasPermi="['store:product-reply:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['store:product-reply:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <ProductReplyForm ref="formRef" @success="getList" />
    </div>
</template>

<script>
import * as ProductReplyApi from '@/api/store/reply';
import ProductReplyForm from './ProductReplyForm.vue';
export default {
  name: "ProductReply",
  components: {
          ProductReplyForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
              // 总条数
        total: 0,
      // 评论列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
                    pageNo: 1,
            pageSize: 10,
        productId: null,
        isReply: null,
        nickname: null,
        createTime: [],
        timeRange: []
      },
            };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
      this.loading = true;
              const params = {
        ...this.queryParams,
        timeRange: this.queryParams.timeRange // 直接传数组
      };
              const res = await ProductReplyApi.getProductReplyPage(params);
        this.list = res.data.list;
        this.total = Number(res.data.total) || 0;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除评论编号为"' + id + '"的数据项?')
      try {
        await ProductReplyApi.deleteProductReply(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有评论数据项?');
      try {
        this.exportLoading = true;
        const data = await ProductReplyApi.exportProductReplyExcel(this.queryParams);
        this.$download.excel(data, '评论.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
  },
  computed: {
    filteredMessageTypes() {
      // 假设 this.getDictDatas(DICT_TYPE.MP_MESSAGE_TYPE) 返回所有选项
      return this.getDictDatas(this.DICT_TYPE.MP_MESSAGE_TYPE)
        .filter(dict => this.requestMessageTypes.includes(dict.value));
    }
  }
};
</script>

<style scoped>
.comment-text {
  display: inline-block;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: left;
}

.nickname-text {
  display: inline-block;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}
</style>
